import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2023-02-07
 * Time:20:08
 */
class Solution {
    public List<String> alertNames(String[] keyName, String[] keyTime) {
        // 哈希表记录每个人名及其时间集合
        HashMap<String, List<String>> map = new HashMap<>();
        int n = keyName.length;
        for (int i = 0; i < n; i++) {
            List<String> timeList = map.getOrDefault(keyName[i], new LinkedList<>());
            timeList.add(keyTime[i]);
            map.put(keyName[i], timeList);
        }
        List<String> ans = new LinkedList<>();
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            String name = entry.getKey();
            List<String> timeList = entry.getValue();
            // 时间排序
            timeList.sort((o1, o2) -> o1.compareTo(o2));
            // 滑动窗口思想，维护一个大小为 3 的窗口 即[i-2, ..., i]
            for (int i = 2; i < timeList.size(); i++) {
                Long t1 = convertTimeToLong(timeList.get(i));
                long t2 = convertTimeToLong(timeList.get(i-2));
                if (t1 - t2 <= 60) {
                    ans.add(name);
                    break;
                }
            }
        }
        // 名称排序
        ans.sort((o1, o2) -> o1.compareTo(o2));
        return ans;
    }
    public Long convertTimeToLong(String time) {
        String[] t = time.split(":");
        return Long.parseLong(t[0]) * 60 + Long.parseLong(t[1]);
    }
}
public class Test {
}
